package com.driving.single.common.sms.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.driving.single.common.redis.service.CommonRedisSendSmsService;
import com.driving.single.common.sms.service.SendSmsService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SendSmsServiceImpl implements SendSmsService {
    private static final String SignName = "附近驾校";  // 短信签名名称

    @Autowired
    private CommonRedisSendSmsService commonRedisSendSmsService;
    /**
     * 注册账号短信验证发送
     * {
     * "Message":"OK",
     * "RequestId":"2184201F-BFB3-446B-B1F2-C746B7BF0657",
     * "BizId":"197703245997295588^0",
     * "Code":"OK"
     * }
     *
     * @param PhoneNumbers 手机号码
     * @return https://help.aliyun.com/document_detail/101346.html?spm=a2c4g.11186623.2.14.238d56e0o00RO4
     */
    @Override
    public String sendRegisterSms(String PhoneNumbers) throws Exception {
        String TemplateCode = "SMS_175540285";
        String code = RandomStringUtils.randomNumeric(4);
        // 将 验证码 存入Redis缓存中
        this.commonRedisSendSmsService.insertRegisterCodeToRedis(PhoneNumbers,code);
        String TemplateParam = "{\"code\":\"" + code + "\"}";
        return this.baseSendSms(PhoneNumbers, TemplateCode, TemplateParam);
    }

    /**
     * 登录账号短信验证码发送
     * @param PhoneNumbers 手机号码
     */
    @Override
    public String sendLoginSms(String PhoneNumbers) throws Exception {
        String TemplateCode = "SMS_175530336";
        String code = RandomStringUtils.randomNumeric(4);
        // 将 验证码 存入Redis缓存中
        this.commonRedisSendSmsService.insertLoginCodeToRedis(PhoneNumbers,code);
        String TemplateParam = "{\"code\":\"" + code + "\"}";
        return this.baseSendSms(PhoneNumbers, TemplateCode, TemplateParam);
    }

    /**
     * 发送短信服务方法
     * @param phoneNumbers  接收短信的手机号码
     * @param templateCode  短信模板ID
     * @param templateParam 短信模板变量对应的实际值，JSON格式
     * @return {"Message":"OK","RequestId":"2184201F-BFB3-446B-B1F2-C746B7BF0657","BizId":"197703245997295588^0",
     * "Code":"OK"}
     */
    private String baseSendSms(String phoneNumbers, String templateCode, String templateParam) throws Exception {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                "LTAI4FcQuPS9bQch4S5mTV6P",
                "Wmu7NhtskIDmeWr8wYVMgNsLOm26vX");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        // 接收短信的手机号码。
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        // 短信签名名称。请在控制台签名管理页面签名名称一列查看。
        request.putQueryParameter("SignName", SendSmsServiceImpl.SignName);
        // 短信模板ID。请在控制台模板管理页面模板CODE一列查看。示范：SMS_153055065
        request.putQueryParameter("TemplateCode", templateCode);
        // 短信模板变量对应的实际值，JSON格式。示范：{"code":"1111"}
        request.putQueryParameter("TemplateParam", templateParam);

        CommonResponse response = client.getCommonResponse(request);
        return response.getData();
    }
}
